package com.lizardmind.haofudoctor.ui.diagnosis.diagnose

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.linxiao.framework.util.ke
import com.linxiao.framework.util.toasts
import com.linxiao.framework.widget.Section.SectionRecyclerHeadersAdapter
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.BaseDocumentsBean
import com.lizardmind.haofudoctor.other.DoctorInfo
import com.lizardmind.haofudoctor.other.Request
import com.lizardmind.haofudoctor.other.TypeDocumentsBean
import com.lizardmind.haofudoctor.ui.diagnosis.diagnose.userFill.FillDiagnoseTwoActivity
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.test_item_problem.view.*
import kotlinx.android.synthetic.main.test_item_problem_header.view.*
import org.jetbrains.anko.collections.forEachWithIndex
import org.jetbrains.anko.find
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.startActivity

/**
 * 发送问诊单适配器
 * Created by Extends on 2017/11/6 11:04
 */
class SendDiagnoseAdapter(val con:Context,val data: ArrayList<BaseDocumentsBean>,val isClick:Boolean = true): RecyclerView.Adapter<SendDiagnoseAdapter.ViewHoder>(),
        SectionRecyclerHeadersAdapter<SendDiagnoseAdapter.HeadViewHodler> {
    private val list= arrayListOf<Array<String>>()//Array<String>{一级id，二级名称}
    init {
        initData(data)
    }

    override fun getItemCount(): Int=list.count()

    override fun onBindViewHolder(holder: ViewHoder, position: Int) {
        holder.name.text = list[position][1]
        holder.itemView.onClick {
            //查看问诊单详情
            con.startActivity<FillDiagnoseTwoActivity>("bean" to data[position],"isClick" to isClick)
        }
        holder.send.onClick {
            //发送问诊单
            if(onSend!=null){
                onSend!!(position,list[position][2])
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHoder {
        return ViewHoder(LayoutInflater.from(parent.context).inflate(R.layout.item_send_diagnose,parent,false))
    }

    override fun getHeaderId(position: Int): Long = list[position][0].toLong()

    override fun onCreateHeaderViewHolder(parent: ViewGroup): HeadViewHodler {
        return HeadViewHodler(LayoutInflater.from(parent.context).inflate(R.layout.item_header_send_diagnose,parent,false))
    }

    override fun onBindHeaderViewHolder(holder: HeadViewHodler, position: Int) {
        (holder.itemView as TextView).text = if(list[position][0]=="0") "系统" else "自定义"
    }

    override fun isNull(position: Int): Boolean = false

    fun addAllAndClear(data:List<BaseDocumentsBean>){
        initData(data)
        notifyDataSetChanged()
    }

    private fun initData(data:List<BaseDocumentsBean>){
        this.data.clear()
        this.data.addAll(data)
        list.clear()
        data.forEach {
            list.add(arrayOf(it.source.toString(),it.title,it.id))
        }
    }

    /**
     * 标准项view
     */
    class ViewHoder(view: View) : RecyclerView.ViewHolder(view){
        val send  = view.find<ImageView>(R.id.isd_send)
        val name  = view.find<TextView>(R.id.isd_name)
    }

    /**
     * 头部view
     */
    class HeadViewHodler(view: View) : RecyclerView.ViewHolder(view)
    var onSend: ((position: Int, data: String) -> Unit)? = null
    var onClick: ((position: Int) -> Unit)? = null
}